package com.yksj.healthtalk.wallet;

import java.util.List;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.CoreConnectionPNames;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;

import com.yksj.healthtalk.net.http.RequestParams;

public class WalletService {
	
	
	public String getWalletBalance() throws Exception{
		return get("http://192.168.16.118:8085/sms_web/GetYellowBoyServlet", "CUSTOMERID=4200");
	}
	
	
	public String getpaypwd() throws Exception{
		RequestParams params = new RequestParams();
		params.put("CUSTOMERID", "4200");
		params.put("TYPE", "SETPASS");
		params.put("PAYMENT_PASS","123456");
		params.put("RE_PAYMENT_PASS", "123456");
		return get("http://192.168.16.118:8085/sms_web/SetWalletInfoServlet", params.toString());
	}
	
	
	public String getchangepwd() throws Exception{
		RequestParams params = new RequestParams();
		params.put("CUSTOMERID", "4200");
		params.put("TYPE", "UPDATEPASS");
		params.put("PAYMENT_PASS","111111"	);
		params.put("RE_PAYMENT_PASS", "111111"	);
		params.put("OLD_PAYMENT_PASS", "123456"	);
		return get("http://192.168.16.118:8085/sms_web/SetWalletInfoServlet", params.toString());
	}
	
	//获取验证码
	public String getCode() throws Exception{
		RequestParams params = new RequestParams();
		params.put("TYPE", "GETCODE");
		params.put("CUSTOMERID", "4200");
		params.put("TELEPHONE", "18811170904");
		return get("http://192.168.16.118:8085/sms_web/SetWalletInfoServlet",params.toString());
	}
	
	
	public String forgetPwd() throws Exception{
		RequestParams params = new RequestParams();
		params.put("TYPE", "FORGETPASS");
		params.put("CUSTOMERID", "4200");
		params.put("PAYMENT_PASS", "123456");
		params.put("RE_PAYMENT_PASS", "123456");
		params.put("CODE", "5009");
		params.put("TELEPHONE", "18811170904");
		return get("http://192.168.16.118:8085/sms_web/SetWalletInfoServlet",params.toString());
	}
	
	public String  post(String uri,List<NameValuePair> params) throws Exception{
		String result = null;
		HttpPost httpPost = new HttpPost(uri);
		 HttpEntity entity = new UrlEncodedFormEntity(params, HTTP.UTF_8);
		// 设置参数实体
	      httpPost.setEntity(entity);
	      // 获取HttpClient对象
	      HttpClient httpClient = new DefaultHttpClient();
	      //连接超时
	      httpClient.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, 30000);
	      //请求超时
	      httpClient.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT, 30000);
	      try {
	       // 获取HttpResponse实例
	          HttpResponse httpResp = httpClient.execute(httpPost);
	          // 判断是够请求成功
	          if (httpResp.getStatusLine().getStatusCode() == 200) {
	           // 获取返回的数据
	           result = EntityUtils.toString(httpResp.getEntity(), "UTF-8");
	          } else {
	        	  result = "HttpPost方式请求失败";
	          }
	      } catch (Exception e){
	       result =  e.toString();
	      }
		return result;
	}

	
        //服务器  ：服务器项目  ：servlet名称  
	public String get(String name,String pwd) throws Exception{  
        String path=name; 
        String uri=path+"?"+pwd;  
        //name:服务器端的用户名，pwd:服务器端的密码  
        //注意字符串连接时不能带空格  
         
        String result="";  
         
        HttpGet httpGet=new HttpGet(uri);//编者按：与HttpPost区别所在，这里是将参数在地址中传递  
        HttpResponse response=new DefaultHttpClient().execute(httpGet);  
        if(response.getStatusLine().getStatusCode()==200){  
                HttpEntity entity=response.getEntity();  
                result=EntityUtils.toString(entity, HTTP.UTF_8);  
        }  
        return uri +"->"+result;  
}  
	
	
}
